package com.sk.bilibili.view.activity;

import static com.sk.bilibili.utils.StringUtils.str;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.sk.bilibili.R;
import com.sk.bilibili.adapter.vlayout.member.SearchAdapter;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.callBack.VideoSetCallBack;
import com.sk.bilibili.databinding.ActivitySpaceSearchBinding;
import com.sk.bilibili.model.activity.SpaceSearchModel;

import java.util.List;

public class SpaceSearchActivity extends BaseActivity<ActivitySpaceSearchBinding, SpaceSearchModel> implements VideoSetCallBack {

    private Integer memberId;

    public static void start(Context context,Integer memberId){
        Intent intent = new Intent(context, SpaceSearchActivity.class);
        intent.putExtra("memberId",memberId);
        context.startActivity(intent);
    }

    @Override
    protected void afterSetView() {
        memberId= (Integer) getIntent().getSerializableExtra("memberId");
        initView();
        bindEvent();
    }

    private void bindEvent() {
        bind.tvSearch.setOnClickListener(v -> model.search(str(bind.etSearch),memberId));
        bind.tvEmpty.setOnClickListener(v -> SearchActivity.start(this,str(bind.etSearch)));
    }

    private SearchAdapter searchAdapter;

    private void initView() {
        showTip();
        VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);
        bind.rvShow.setLayoutManager(layoutManager);
        DelegateAdapter adapter = new DelegateAdapter(layoutManager);
        searchAdapter=new SearchAdapter(this,this);
        adapter.addAdapter(searchAdapter);
        bind.rvShow.setAdapter(adapter);
    }

    private void showTip() {
        hideAll();
        bind.tvTip.setVisibility(View.VISIBLE);
    }

    private void hideAll() {
        bind.rvShow.setVisibility(View.GONE);
        bind.ivEmpty.setVisibility(View.GONE);
        bind.tvEmpty.setVisibility(View.GONE);
        bind.tvTip.setVisibility(View.GONE);
    }

    public void searchSuccess(List<VideoSet> records) {
        if(records.isEmpty()){
            showEmpty();
        }else {
            showList();
            searchAdapter.setData(records);
        }
    }

    private void showList() {
        hideAll();
        bind.rvShow.setVisibility(View.VISIBLE);
    }

    private void showEmpty() {
        hideAll();
        bind.ivEmpty.setVisibility(View.VISIBLE);
        bind.tvEmpty.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClickVideoSet(Integer id) {
        VideoSetActivity.start(this,id);
    }

}